
/*
 * 初步封装按钮控件,用于点击后显示数字倒数功能
 * reClass用于覆盖默认样式
 * 调用方法
 * <ReMsgButton reButtonClick={this.test} reClass="dark" count={120}>testdsfds</ReMsgButton>
 * reButtonClick需有返回值
 * 样式表与ReButton共用
 * */
'use strict';
var React = require('react');
class ReMsgButton extends React.Component {
    constructor(props) {
        super(props);
        this.getSpecialClass=this.getSpecialClass.bind(this);
        this.state={
            count:0
        };
        this.buttonClick=this.buttonClick.bind(this);
    }


    //按钮点击事件
    buttonClick(){
        //仅当父控件传入函数返回true，表示检测通过，正确调用时才出现倒数
        if(this.props.reButtonClick()) {
            this.setState({
                count: this.props.count
            });
            var interval = setInterval(()=> {
                var currentCount = this.state.count - 1;
                this.setState({
                    count: currentCount
                });
                if (currentCount == 0) {
                    clearInterval(interval);
                }
            }, 1000);
        }

    }


    //获取样式
    getSpecialClass(){
        if(this.state.count==0) {
            if (this.props.reClass != undefined) {
                return "ReButton " + this.props.reClass;
            }
            else {
                return 'ReButton';
            }
        }
        else{
            if (this.props.reClass != undefined) {
                return "ReButton " + this.props.reClass+" buttonUnable";
            }
            else {
                return 'ReButton buttonUnable';
            }
        }
    }

    //获取倒数样式
    getCountClass(){
        if(this.state.count==0) {
            return 'Hide';
        }
        else{
            return 'MsgCount'
        }
    }

    render(){
        return (
            <button className={this.getSpecialClass()} disabled={this.state.count==0?false:'disabled'} onClick={this.buttonClick}><div>{this.props.children}</div><div className={this.getCountClass()}>重新获取({this.state.count})</div></button>
        );
    }
}

module.exports=ReMsgButton;